package com.wjf.resourceservice.mapper;

import com.wjf.resourceservice.entity.Music;
import org.apache.ibatis.annotations.*;
import java.util.Date;
import java.util.List;

/**
 * 音乐数据访问层接口
 * 处理音乐相关的数据库操作
 */
@Mapper
public interface MusicMapper {
    
    /**
     * 分页获取音乐列表
     * @param offset 偏移量
     * @param size 每页大小
     * @return 音乐列表
     */
    @Select("SELECT * FROM music WHERE status = 1 ORDER BY create_time DESC LIMIT #{offset}, #{size}")
    List<Music> getMusicList(@Param("offset") int offset, @Param("size") int size);
    
    /**
     * 根据ID获取音乐详情
     * @param id 音乐ID
     * @return 音乐信息
     */
    @Select("SELECT * FROM music WHERE id = #{id} AND status = 1")
    Music getMusicById(Long id);
    
    /**
     * 增加音乐播放次数
     * @param id 音乐ID
     * @return 影响的行数
     */
    @Update("UPDATE music SET play_count = play_count + 1 WHERE id = #{id}")
    int incrementPlayCount(Long id);
    
    /**
     * 获取音乐总数
     * @return 音乐总数
     */
    @Select("SELECT COUNT(*) FROM music WHERE status = 1")
    int getTotalCount();

    @Insert("INSERT INTO music (title, artist, album, duration, cover_url, music_url, " +
            "lyric_url, status, vip_only, create_time) " +
            "VALUES (#{title}, #{artist}, #{album}, #{duration}, #{coverUrl}, #{musicUrl}, " +
            "#{lyricUrl}, #{status}, #{vipOnly}, #{createTime})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int addMusic(Music music);

    int updateMusic(Music music);

    @Update("UPDATE music SET status = 0, update_time = #{updateTime} WHERE id = #{id}")
    int deleteMusic(@Param("id") Long id, @Param("updateTime") Date updateTime);
} 